package com.yonyou.pmclouds.billview.rmiimpl.buzi;

import com.yonyou.pmclouds.basecom.billstatus.BillStatusConst;
import com.yonyou.pmclouds.basecom.consts.BillTypeConst;
import com.yonyou.pmclouds.basecom.entity.PageObject;
import com.yonyou.pmclouds.basecom.entity.SuperVO;
import com.yonyou.pmclouds.basecom.exception.BusinessException;
import com.yonyou.pmclouds.billview.entity.BillViewQueryVO;
import com.yonyou.pmclouds.billview.rmitf.IBillViewBuziService;
import com.yonyou.pmclouds.billview.util.BillViewUtil;
import com.yonyou.pmclouds.workstopreport.entity.WorkStopVO;
import com.yonyou.pmclouds.workstopreport.rmiitf.WorkStopQuery;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class WorkStopBuziServiceImpl implements IBillViewBuziService {

    @Autowired
    private WorkStopQuery workStopQuery;

    @Override
    public String getBillType() {
        return BillTypeConst.WORKSTOP_BILLTYPE;
    }


    @Override
    public PageObject<Object> queryBillList(BillViewQueryVO billViewQueryVO) throws BusinessException {
        String startTime = StringUtils.isNotBlank(billViewQueryVO.getStartTime()) ? billViewQueryVO.getStartTime() + " 00:00:00" : "1000-01 00:00:00";
        String endTime = StringUtils.isNotBlank(billViewQueryVO.getEndTime()) ? billViewQueryVO.getEndTime() + " 23:59:59" :"2090-12-31 23:59:59";
        billViewQueryVO.setStartTime(startTime);
        billViewQueryVO.setEndTime(endTime);
        PageObject<Object> workStopVOs = workStopQuery.queryByCondition(billViewQueryVO);
        BillViewUtil.setBillStatus(workStopVOs.getObjects());
        return workStopVOs;
    }

    @Override
    public SuperVO queryBillDetail(String pkBill) throws BusinessException {
        WorkStopVO workStopVO = workStopQuery.getReport(pkBill);

        //判断单据状态
        if (workStopVO.getBillstatus() != null) {
            switch (workStopVO.getBillstatus()) {
                case BillStatusConst.COMMON_APPROVING_STATUS:
                    workStopVO.setStatus("审批中");
                    break;
                case BillStatusConst.COMMON_REJECT_STATUS:
                    workStopVO.setStatus("驳回");
                    break;
                case BillStatusConst.COMMON_APPROVED_STATUS:
                    workStopVO.setStatus("审批通过");
                    break;
                case BillStatusConst.COMMON_COMMIT_STATUS:
                    workStopVO.setStatus("驳回后再提交");
                    break;
                case BillStatusConst.COMMON_FREE_STATUS:
                    workStopVO.setStatus("自由态");
            }
        }
        return workStopQuery.getReport(pkBill);
    }
}
